<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="dataForm.username" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pwd">
      <el-input v-model="dataForm.pwd" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="地区id" prop="aId">
      <el-input v-model="dataForm.aId" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="1 管理员;2 校园代理;3 代理子账户;4 普通账户" prop="dtype">
      <el-input v-model="dataForm.dtype" placeholder="1 管理员;2 校园代理;3 代理子账户;4 普通账户"></el-input>
    </el-form-item>
    <el-form-item label="1 可用;2 不可用" prop="userState">
      <el-input v-model="dataForm.userState" placeholder="1 可用;2 不可用"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建日期"></el-input>
    </el-form-item>
    <el-form-item label="1 删除;2 没有删除" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="1 删除;2 没有删除"></el-input>
    </el-form-item>
    <el-form-item label="账户余额" prop="payment">
      <el-input v-model="dataForm.payment" placeholder="账户余额"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="代理期限" prop="deadline">
      <el-input v-model="dataForm.deadline" placeholder="代理期限"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          pwd: '',
          aId: '',
          dtype: '',
          userState: '',
          createTime: '',
          isDelete: '',
          payment: '',
          phone: '',
          deadline: ''
        },
        dataRule: {
          username: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          pwd: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          aId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          dtype: [
            { required: true, message: '1 管理员;2 校园代理;3 代理子账户;4 普通账户不能为空', trigger: 'blur' }
          ],
          userState: [
            { required: true, message: '1 可用;2 不可用不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '1 删除;2 没有删除不能为空', trigger: 'blur' }
          ],
          payment: [
            { required: true, message: '账户余额不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          deadline: [
            { required: true, message: '代理期限不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.user.username
                this.dataForm.pwd = data.user.pwd
                this.dataForm.aId = data.user.aId
                this.dataForm.dtype = data.user.dtype
                this.dataForm.userState = data.user.userState
                this.dataForm.createTime = data.user.createTime
                this.dataForm.isDelete = data.user.isDelete
                this.dataForm.payment = data.user.payment
                this.dataForm.phone = data.user.phone
                this.dataForm.deadline = data.user.deadline
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'pwd': this.dataForm.pwd,
                'aId': this.dataForm.aId,
                'dtype': this.dataForm.dtype,
                'userState': this.dataForm.userState,
                'createTime': this.dataForm.createTime,
                'isDelete': this.dataForm.isDelete,
                'payment': this.dataForm.payment,
                'phone': this.dataForm.phone,
                'deadline': this.dataForm.deadline
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
